﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Net
{
    using System;

    using Phuse.UnitTesting;

    /// <summary>Provides helper methods to execute the same method multiple times with different responses.</summary>
    /// <typeparam name="TResult">The type of the result that the executed method will produce.</typeparam>
    internal abstract class ExecuteHelperBase<TResult>
    {
        private readonly FakeServer server;
        private readonly Func<TResult> method;
        private readonly string expectedCommand;

        internal ExecuteHelperBase(FakeServer server, Func<TResult> method, string expectedCommand)
        {
            this.server = server;
            this.method = method;
            this.expectedCommand = expectedCommand;
        }

        internal TResult Execute(string response)
        {
            this.server.QueueCommand(this.expectedCommand, response);
            return this.method();
        }

        internal void AssertThrow<TException>(string response) where TException : Exception
        {
            this.server.QueueCommand(this.expectedCommand, response);
            ExecuteHelperBase<TResult> executer = this;
            TestBase.AssertThrow<TException>(() => executer.method());
        }
    }
}
